<?php
class koudai_orderControl extends skymvc{
	private $admin;
	public function __construct(){
		parent::__construct();
	}
	
	public function onInit(){
		$this->loadConfig("table");
		$this->admin=M("login")->getKoudaiAdmin();
		
		//检查签单员权限
		if($this->admin['typeid']!=1){
			$order_id=get_post('order_id','i');
			if(empty($order_id)) $this->goAll("您无权限",0,0,APPKOUDAI."?m=koudai_qd");
			$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
			if($data['qdadmin']!=ADMINID) $this->goAll("您无权限",0,0,APPKOUDAI."?m=koudai_qd");
			$this->smarty->assign("qdadmin",1);
		}
	}
	
	public function onNewJs(){
		$where=" shopid=".SHOPID." AND status =0 ";
		$row=M("koudai_order")->selectRow($where);
		if($row){
			exit(json_encode(array("success"=>1)));
		}else{
			exit(json_encode(array("success"=>0)));
		}
	}
	
	public function onDefault(){
		  
		$where=" shopid=".SHOPID." AND (isadd=0 or(isadd=1 AND isdz=1) ) ";
		$url=APPKOUDAI."?m=koudai_order";
		$start=get('per_page','i');
		$limit=100;
		$status=get('status','i');
		$where.=" AND status=$status";
		$url.="&status=$status";
		$index='';
		switch(get('index')){
			case 'asc':
					$index='asc';
				break;
			case 'desc':
					$index='desc';
				break;
		}
		switch(get('orderby')){
			case "id":
				$order=" order_id $index";
				break;
			case "sushe":
				$order=" sushe $index";
				break;
			default:
				$order=" order_id DESC";
				break;
		}
		$option=array(
			"where"=>$where,
			"order"=>$order, 
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		$scorder=array();
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));	
				$v['num']=M("koudai_order")->getOne("select sum(amount) as ct from ".table('koudai_order_product')." where order_id=".$v['order_id']." ");
				$scids[]=$v['sc_id'];
				$data[$k]=$v;
			}
			$sitecitys=M("site_city")->id_list(array("where"=>"siteid=".SITEID));
			foreach($data as $v){
				 
				$scorder[$v['sc_id']]['order'][]=$v;
			}
			
		}
		 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"baseurl"=>$url,
			"scorder"=>$scorder,
			"sitecitys"=>$sitecitys,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_order/index.html");
	}
	
	public function onDefaultOld(){
		$status_w=" AND status in(0,1,2,3,10)  "; 
		$where=" shopid=".SHOPID." AND (isadd=0 or(isadd=1 AND isdz=1) ) ";
		$url=APPKOUDAI."?m=koudai_order";
		$start=get('per_page','i');
		$limit=20;
		
		$orderno=get('orderno','h');
		if($orderno){
			$where.=" AND orderno='".$orderno."' ";
			$url.="&orderno=$orderno";
		}
		
		$nickname=get('ninckname','h');
		if($nickname){
			$user=M("user")->selectRow(array("where"=>"nickname='".$nickname."' "));
			if($user){
				$where.=" userid=".$user['userid'];
			}else{
				$where.=" 1=2 ";			
			}
			$url.="&nickname=".urlencode($nickname);
		}
		 
		$status=get('s_status','i');
		if($status>=0){
			$status_w=" AND status=$status";
			$url.="&s_status=$status";
		}
		if(!isset($_GET['s_ispay'])){
			$ispay=-1;
		}else{
			$ispay=get('s_ispay','i');
		}
		if($ispay>=0){
			$where.=" AND ispay=$ispay";
			$url.="&s_ispay=$ispay"; 
		}
		$start_time=get('start_time','h');
		$end_time=get('end_time','h');
		if($start_time){
			$where.=" AND dateline>".strtotime($start_time)." ";
			$url.="&start_time=".$start_time;
		}
		
		if($end_time){
			$where.=" AND dateline<".strtotime($end_time)." ";
			$url.="&end_time=".$end_time;
		}
		$type_id=get_post('type_id','i');
		if($type_id){
			$where.=" AND type_id=".$type_id." ";
			$url.="&type_id=".$type_id;
		}
		$isdz=get('isdz','i');
		if($isdz){
			$where.=" AND isdz=1  ";
			$url.="&isdz=1";
		}
		$isqd=get('isqd','i');
		if($isqd){
			$where.=" AND qdadmin=0 ";
			$status_w=" AND status=1 ";
			$url.="&isqd=1";
		}
		$where.=$status_w;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));
				$data[$k]=$v;
			}
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		if($isdz){
			$this->smarty->display("koudai_order/duizhang.html");
		
		}else{
			$this->smarty->display("koudai_order/indexold.html");
		}
	}
	/*
	*订单详情
	*/
	public function onShow(){
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		$addr=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$order_id,"order"=>"id DESC"));
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		//获取商品
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".$data['order_id']." AND gzproduct=0 " ));
		 
		$total_num=0;
		foreach($order_product as $k=>$v){
			$p=M('product')->selectRow(array("where"=>"id=".$v['productid']));
			$ks=M('product_ks')->selectRow("id=".$v['ksid']);
			if($ks){
				$p['ks_title']=$ks['title'];
				$p['price']=$ks['lower_price']>0?$ks['lower_price']:$ks['price'];
				$p['price']=$ks['price'];
			}else{
				$p['price']=$p['lower_price']>0?$p['lower_price']:$p['price'];
			}
			$p['order_price']=$v['price'];
	 		$p['amount_send']=$v['amount_send'];
			$p['amount']=$v['amount'];
			$total_num+=$v['amount'];
			$order_product[$k]=$p;
		}
		$data['total_num']=$total_num;
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"total_num"=>$total_num,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"order_product"=>$order_product,
			"admin"=>$this->admin,
			"pay_type_list"=>pay_type_list(),
			"shop"=>M("koudai")->selectRow("id=".$data['shopid']) 
		));
		$this->smarty->display("koudai_order/show.html");
		
	}
	/**
	*订单确认
	*/
	public function onConfirm(){
		$order_id=get_post('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if(!$data['unpay'] && $data['ispay']!=2){
			$this->goAll("还未支付，不能确认订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		M("koudai_order")->update(array("status"=>1),"order_id=".$order_id);
		$content=post('content');
		M("koudai_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content,
		));
		$msg=array(
			"dateline"=>time(),
			"type_id"=>1,
			"status"=>0,
			"userid"=>$data['userid'],
			"content"=>"订单<a href='/index.php?m=koudai_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已确认",
		);
		$msg['id']=M("notice")->insert($msg);
		$this->goall("确认成功");
	}
	/**
	*订单发送
	*/
	public function onSend(){
		$order_id=get_post('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if(!$data['unpay'] && $data['ispay']!=2){
			$this->goAll("还未支付，不能发货订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		$content=post('content');
		$shipping_info=post('shipping_info','h');
		$kdy_telephone=post('kdy_telephone','h');
		if($data['status']<2){
			M("koudai_order")->update(array("status"=>2),"order_id=".$order_id);
		}
		
		M("koudai_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."快递信息：".$shipping_info,
		));
		M("koudai_order")->update(array("shipping_info"=>$shipping_info,"kdy_telephone"=>$kdy_telephone),"order_id=".$order_id);
		$content="您的口袋店订单<a href='/index.php?m=ymdian_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已发货";
		M("notice")->add(array(
				"content"=>$content,
				"userid"=>$data['userid'],
		));
		$this->goall("发货成功");
	}

	/**
	*订单完成
	*/
	public function onFinish(){
		$order_id=get_post('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if(!$data['unpay'] && $data['ispay']!=2){
			$this->goAll("还未支付，不能完成订单");
		}
		if($data['shopid']!=SHOPID){
			$this->goAll("暂无权限");
		}
		/*
		if($data['isreceived']!=2 ){
			 $this->goall("未收货不能确定完成",1);
		}
		*/
		if($data['status']>=3){
			$this->goALl("参数出错",1);
		}
		M("koudai_order")->begin();
		M("koudai_order")->update(array("status"=>3),"order_id=".$order_id);
		$content=post('content');
		M("koudai_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content,
		));
		
		//非分期订单 处理收入分配
		//添加产品销售数量
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		foreach($order_product as $k=>$v){
			$product=M("product")->selectRow("id=".$v['productid']);
			M("koudai_product")->changenum("buy_num",$v['amount'],"id=".$v['object_id']);
			//增加统计数量
			$option=array(
				"siteid"=>$data['siteid'],
				"shopid"=>$data['shopid'],
				"k"=>"koudai",
				"time"=>$data['dateline'],
				"productid"=>$v['productid'],
				"ordernum"=>$v['amount'],
				"money"=>$v['amount']*$v['price'],
				"costmoney"=>$v['amount']*$product['gys_price'],
				"profitmoney"=>$v['amount']*$v['price']-$v['amount']*$product['gys_price']
			);
			M("koudai_product")->statDayAdd($option);
			M("koudai_product")->statMonthAdd($option);
			M("koudai_product")->statWeekAdd($option);
			M("product")->changenum("buy_num",$v['amount'],"id=".$v['productid']);
			if($v['ksid']){
				M('product_ks')->changenum("buy_num",$v['amount'],"id=".$v['ksid']);
			}
		}
		$money1=$data['money'];
		if(!$data['fenqi']){
		 
			$koudai=M("koudai")->selectRow("id=".$data['shopid']);
			M("koudai")->addMoney(array(
					"shopid"=>$koudai['id'],
					"income"=>-$money1,
					"content"=>"您的订单完成了，获得[money]元收入"
			));
			//添加今日收入
			$kday=date("Ymd",$data['dateline']);
			$rd=M("stat_shoporder_day")->selectRow(" shopid=".$data['shopid']." AND k='koudai' AND kday='".$kday."' ");
			$on_money=$off_money=0;
			if($data['ispay']==2){
				$on_money=$data['money'];
			}else{
				$off_money=$data['money'];
			}
			if($rd){
				M("stat_shoporder_day")->update(array(
					"money"=>$money1+$rd['money'],
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_day")->insert(array(
					"money"=>$money1,
					"ordernum"=>1,
					"kday"=>$kday,
					"siteid"=>$data['siteid'],
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
					"k"=>"koudai",
					"shopid"=>$data['shopid']
				));
			}
			//添加本周收入
			$kweek=date("YW",$data['dateline']);
			$rd=M("stat_shoporder_week")->selectRow(" shopid=".$data['shopid']." AND k='koudai' AND kweek='".$kweek."' ");
			if($rd){
				M("stat_shoporder_week")->update(array(
					"money"=>$money1+$rd['money'],
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
					"ordernum"=>$rd['ordernum']+1
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_week")->insert(array(
					"money"=>$money1,
					"ordernum"=>1,
					"kweek"=>$kweek,
					"siteid"=>$data['siteid'],
					"k"=>"koudai",
					"shopid"=>$data['shopid'],
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
				));
			}
			
			//添加本月收入
			$kmonth=date("Ym",$data['dateline']);
			$rd=M("stat_shoporder_month")->selectRow(" shopid=".$data['shopid']." AND k='koudai' AND kmonth='".$kmonth."' ");
			if($rd){
				M("stat_shoporder_month")->update(array(
					"money"=>$money1+$rd['money'],
					"ordernum"=>$rd['ordernum']+1,
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
				),"id=".$rd['id']);
			}else{
				M("stat_shoporder_month")->insert(array(
					"money"=>$money1,
					"ordernum"=>1,
					"kmonth"=>$kmonth,
					"siteid"=>$data['siteid'],
					"k"=>"koudai",
					"shopid"=>$data['shopid'],
					"on_money"=>$rd['on_money']+$on_money,
					"off_money"=>$rs['off_money']+$off_money,
				));
			}
			
		}
			
			
		 
		//处理口袋收入结束
		
		//添加积分
		$this->loadControl("jfapi","source/index/");
		$this->jfapiControl->setUserId($data['userid'])->addGrade(array(
					"grade"=>$data['money'],
					"type_id"=>2,
					"content"=>"您的订单完成获得了".$data['money']."积分，之前有[oldgrade]分，目前有[newgrade]分",
		));
		$msg=array(
			"dateline"=>time(),
			"type_id"=>1,
			"status"=>0,
			"userid"=>$data['userid'],
			"content"=>"订单<a href='/index.php?m=koudai_order&a=show&order_id=".$data['order_id']."' target='_blank'>".$data['orderno']."</a>已完成",
		);
		$msg['id']=M("notice")->insert($msg);
		M("koudai_order")->commit();
		$this->goall("订单完成");
	}
	
	/**
	*取消订单
	*/
	 	
	public function onCancel(){
		
		$order_id=get_post('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['status']>=3) $this->goall("操作失败",1);
		M("koudai_order")->begin();
		M("koudai_order")->update(array("status"=>10,"whocancel"=>"koudaiadmin"),"order_id=".$order_id);
		$content=post('content');
		$message=post('message','h');
		M("koudai_order_log")->insert(array(
			"dateline"=>time(),
			"admin_id"=>$this->admin['id'],
			"order_id"=>$order_id,
			"content"=>$content."，原因：".$message,
		));
		//发送通知
		/*取消上单对账添加的产品*/
		if($data['fenqi']){
			$lastorders=json_decode(base64_decode($data['lastorders']));
			if($lastorders){
				foreach($lastorders as $v){
					$lastorder=M("koudai_order")->update(array(
						"isadd"=>0
					),"order_id=".$v);
				}
			}
			
		}
	 	$content=$content."，原因：".$message;
		M("notice")->add(array(
				"content"=>$content,
				"userid"=>$data['userid'],
		));
	 
		//退钱给用户
		if($data['fenqi']){
			M("youka")->addMoney(array(
					"userid"=>$data['userid'],
					"money"=>$data['money'],
					"typeid"=>$data['fenqi'],
					"content"=>"您的订单被取消了返还[money]元",	
			));
		}else{
			if($data['ispay']==2){
				/**$row=M("recharge_back")->selectRow(" order_id=".$order_id." AND tablename='koudai' ");
				if(empty($row)){
					M("recharge_back")->insert(array(
						"order_id"=>$order_id,
						"tablename"=>"koudai",
						"paytype"=>$data['paytype'],
						"dateline"=>time(),
						"ip"=>ip(),
						"admin_id"=>$this->admin['id'],
					));
				}
				**/
				M("user")->addMoney(array(
						"userid"=>$data['userid'],
						"money"=>$data['money'],
						"content"=>"您的订单被取消了返还[money]元",			
				));
				
			}
		}
		//加产品
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		foreach($order_product as $k=>$v){
			M("koudai_product")->total_num_add(array(
					"shopid"=>$data['shopid'],
					"productid"=>$v['productid'],
					"total_num"=>$v['amount'],
					"content"=>"[订单取消]"
			));
		}
		M("koudai_order")->commit();
		$this->goall("取消成功");
	}

	 
	
}
?>